package com.ysd.lis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysInterfaceDictField;
import com.ysd.lis.mapper.sys.SysInterfaceDictFieldMapper;
import com.ysd.lis.request.*;
import com.ysd.lis.service.SysInterfaceDictFieldService;
import com.ysd.util.ToolsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2022-02-05
 */
@Service
@Slf4j
public class SysInterfaceDictFieldServiceImpl extends ServiceImpl<SysInterfaceDictFieldMapper, SysInterfaceDictField> implements SysInterfaceDictFieldService {

    @Autowired
    SysInterfaceDictFieldMapper sysInterfaceDictFieldMapper;

    @Override
    public Result findSysParamGroupList(SysParamFieldParam param) {
        LambdaQueryWrapper<SysInterfaceDictField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysInterfaceDictField::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SysInterfaceDictField::getTitle, param.getSeachValue())
                    .or().like(SysInterfaceDictField::getCode, param.getSeachValue()));
        }
        if (null != param.getGrade()) {
            lambdaQueryWrapper.eq(SysInterfaceDictField::getGrade, param.getGrade());
        }
        if (StringUtils.isNotBlank(param.getPid())) {
            lambdaQueryWrapper.eq(SysInterfaceDictField::getPid, param.getPid());
        }
        lambdaQueryWrapper.orderByAsc(SysInterfaceDictField::getSeq);
        List<SysInterfaceDictField> fieldList = sysInterfaceDictFieldMapper.selectList(lambdaQueryWrapper);
        if (param.getGrade() == 0) {
            for (SysInterfaceDictField field : fieldList) {
                field.setEditFormItemIndex(0);
            }
        }

        return Result.succ(1, "查询成功", fieldList);
    }

    @Override
    public Result findInterfaceDictFieldList(InterfaceDictParam param) throws JsonProcessingException {
        LambdaQueryWrapper<SysInterfaceDictField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysInterfaceDictField::getDelFlag, 0);
        if (StringUtils.isNotBlank(param.getSeachValue())) {
            lambdaQueryWrapper.and(wrapper -> wrapper.like(SysInterfaceDictField::getTitle, param.getSeachValue())
                    .or().like(SysInterfaceDictField::getCode, param.getSeachValue()));
        }
        if (null != param.getGrade()) {
            lambdaQueryWrapper.eq(SysInterfaceDictField::getGrade, param.getGrade());
        }
        if (StringUtils.isNotBlank(param.getPid())) {
            lambdaQueryWrapper.eq(SysInterfaceDictField::getPid, param.getPid());
        }
        if (StringUtils.isNotBlank(param.getId())) {
            lambdaQueryWrapper.eq(SysInterfaceDictField::getId, param.getId());
        }
        lambdaQueryWrapper.orderByAsc(SysInterfaceDictField::getSeq);
        List<SysInterfaceDictField> fieldList = sysInterfaceDictFieldMapper.selectList(lambdaQueryWrapper);

        List<SysParamFieldDto> fmItems = new ArrayList<>();
        Integer j = 0;
        for (SysInterfaceDictField item : fieldList) {
            SysParamFieldDto fmitm = new SysParamFieldDto();
            BeanUtil.copyProperties(item, fmitm, "fieldOptions");
            fmitm.setVisible(1);
            fmitm.setIsCurrent(j == 0);
            if (ToolsUtils.isNotEmpty(item.getFieldSource()) && item.getFieldSource().equals("3") && ToolsUtils.isNotEmpty(item.getFieldOptions())) {
                ObjectMapper objectMapper = new ObjectMapper();
                SysFieldOptionsDto[] ops = objectMapper.readValue(item.getFieldOptions(), SysFieldOptionsDto[].class);
                fmitm.setFieldOptions(Arrays.asList(ops));
            }
            fmItems.add(fmitm);
            j++;
        }

        return Result.succ(1, "查询成功", fmItems);
    }

    @Override
    public Result addSysParamField(SysInterfaceDictField field) {
        int insert = sysInterfaceDictFieldMapper.insert(field);
        if (insert > 0) {
            return Result.succ(1, "添加成功", field);
        } else {
            return Result.fail(400, "添加失败", "");
        }
    }

    @Override
    public Result editSysParamField(SysInterfaceDictField field) {
        int updateById = sysInterfaceDictFieldMapper.updateById(field);
        if (updateById > 0) {
            return Result.succ(1, "提交成功", field);
        } else {
            return Result.fail(400, "修改失败", "");
        }
    }

    @Override
    public Result deleteSysParamField(SysInterfaceDictField field) {
        if (field.getGrade() == 0) {
            LambdaQueryWrapper<SysInterfaceDictField> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysInterfaceDictField::getDelFlag, 0).eq(SysInterfaceDictField::getPid, field.getId());
            Integer cnt = sysInterfaceDictFieldMapper.selectCount(queryWrapper);
            if (cnt > 0) {
                return Result.fail("分组下有参数值，不能删除");
            }
        }
        int deleteById = sysInterfaceDictFieldMapper.deleteById(field.getId());
        if (deleteById > 0) {
            return Result.succ(1, "删除成功", "");
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result saveSysParamFieldList(SysParamDto dto) throws JsonProcessingException {
        //先根据pid删除所有得数据
        LambdaQueryWrapper<SysInterfaceDictField> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysInterfaceDictField::getPid, dto.getId());
        int delete = sysInterfaceDictFieldMapper.delete(lambdaQueryWrapper);
        List<SysParamFieldDto> paramFieldList = dto.getParamFieldList();

        List<SysInterfaceDictField> addList = new ArrayList<>();
        for (SysParamFieldDto fieldDto : paramFieldList) {
            SysInterfaceDictField sysParamField = new SysInterfaceDictField();
            BeanUtil.copyProperties(fieldDto, sysParamField);
            if (ToolsUtils.isNotEmpty(fieldDto.getFieldSource()) && ToolsUtils.isNotEmpty(fieldDto.getFieldOptions())) {
                ObjectMapper objectMapper = new ObjectMapper();
                sysParamField.setFieldOptions(objectMapper.writeValueAsString(fieldDto.getFieldOptions()));
            }
            addList.add(sysParamField);
        }

        boolean b = this.saveOrUpdateBatch(addList);
        if (b) {
            return Result.succ(1, "成功", null);
        } else {
            return Result.fail(400, "失败", "");
        }

    }


}
